package com.bdqn.boot.yuanshen.controller;

import com.bdqn.boot.yuanshen.bean.Videos;
import com.bdqn.boot.yuanshen.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Hasee
 * @version 1.0
 * @className MinioCotroller
 * @description TODD
 * @date 2025/3/5 10:32
 */
@RestController
@RequestMapping("/minio")
public class MinioCotroller {
    @Autowired
    private MinioService minioService;

    /**
     * 上传视频
     */
    @PostMapping("/upload")
    public ResponseEntity<String> uploadVideo(
            @RequestParam("file") MultipartFile file,
            @RequestParam("bucket") String bucket,
            @RequestParam("title") String title,
            @RequestParam("description") String description,
            @RequestParam("coverUrl") String coverUrl) {
        String videoUrl = minioService.uploadVideo(bucket, file, title, description, coverUrl);
        return videoUrl != null
                ? ResponseEntity.ok("视频上传成功: " + videoUrl)
                : ResponseEntity.status(500).body("视频上传失败");
    }

    /**
     * 删除视频
     */
    @DeleteMapping("/delete/{bucket}/{fileName}/{videoId}")
    public ResponseEntity<String> deleteVideo(
            @PathVariable String bucket,
            @PathVariable String fileName,
            @PathVariable int videoId) {
        boolean deleted = minioService.deleteVideo(bucket, fileName, videoId);
        return deleted
                ? ResponseEntity.ok("视频删除成功")
                : ResponseEntity.status(500).body("视频删除失败");
    }

    /**
     * 更新视频（支持更换视频文件）
     */
    @PutMapping("/update")
    public ResponseEntity<String> updateVideo(
            @RequestPart("video") Videos video,
            @RequestPart(value = "file", required = false) MultipartFile file) {
        boolean success = minioService.updateVideo(video, file);
        return success
                ? ResponseEntity.ok("视频信息更新成功")
                : ResponseEntity.status(500).body("视频信息更新失败");
    }
}
